package reduce.fz.com.reduce.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.WitnessCardReply;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 *
 */
public class WitnessCardReplyFragment extends BaseListFragment<WitnessCardReplyFragment.ReplyViewHolder, WitnessCardReply> {
    private Long cardid;
    private LayoutInflater layoutInflater;

    public WitnessCardReplyFragment() {

    }

    public void addReply(WitnessCardReply reply)

    {
        setNoMore();
        baseListAdapter.addData(reply);
        scrollTo(getSize() - 1);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        this.layoutInflater = LayoutInflater.from(view.getContext());
        cardid = getArguments().getLong("cardid");
        return view;


    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    @Override
    public ReplyViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.story_reply_item, parent, false);
        ReplyViewHolder holder = new ReplyViewHolder(view);
        return holder;
    }

    @Override
    public ReplyViewHolder createfootViewHolder(View view) {
        return new ReplyViewHolder(view);
    }

    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<WitnessCardReply>>(new TypeReference<Result<List<WitnessCardReply>>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<List<WitnessCardReply>> data) {
                List<WitnessCardReply> datas = data.getData();
                addResult(data);

            }
        });
    }


    @Override
    protected String setUrlBeforeRequest(List<WitnessCardReply> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_witness_comments_list, cardid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_PAGE_NO;
    }


    /**
     * 创建故事回复fragment
     *
     * @param storid
     * @return
     */
    public static WitnessCardReplyFragment create(Long storid) {
        Bundle bundle = new Bundle();
        bundle.putLong("cardid", storid);
        WitnessCardReplyFragment fragment = new WitnessCardReplyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 创建header
     */
    public class ReplyViewHolder extends BaseRecycleViewHolder<WitnessCardReply> {
        private TextView mTvNickName;
        private TextView mTvReplyTime;
        private TextView mTvReplyContent;
        private RoundNetworkImageView mIvUserHeader;

        public ReplyViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        public void initWidget(View view) {
            mTvNickName = (TextView) view.findViewById(R.id.tv_user_nickname);
            mTvReplyTime = (TextView) view.findViewById(R.id.tv_reply_time);
            mTvReplyContent = (TextView) view.findViewById(R.id.tv_reply_content);
            mIvUserHeader = (RoundNetworkImageView) view.findViewById(R.id.iv_user_header);

        }

        @Override
        public void renderView(WitnessCardReply data, int position) {
            if (data == null) return;
            mTvNickName.setText(data.user == null ? null : data.user.nickname);
            mTvReplyTime.setText(DateUtil.getInterval(data.replytime));
            mTvReplyContent.setText(data.description);
            mIvUserHeader.setImageUrl(data.user.avatar,HttpUtils.getAvatarImageLoader());
        }
    }

}
